﻿package com.wandian.view.mediator
{
	import com.greensock.TweenLite;
	import com.wandian.controller.*;
	import com.wandian.data.GameData;
	import com.wandian.model.*;
	import com.wandian.model.vo.*;
	import com.wandian.util.TextControl;

	import flash.display.MovieClip;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;

	import org.puremvc.as3.interfaces.*;
	import org.puremvc.as3.patterns.mediator.*;

	/**
	 * 聊天提示框
	 * @author long
	 * @Create  2012-10-23
	 * @QQ 108232706
	 * @version 1.0
	 */
	public class ChatTipMediator extends Mediator
	{
		private var _arrChatTip:Array;
		private var _inRoomData:InRoomPlayerDataProxy;
		private var _maxWidth:int=85;
		private var _maxHeight:int=35;
		public static const NAME:String="ChatTipMediator";

		public function ChatTipMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
			return;
		}

		override public function onRegister():void
		{
			this._inRoomData=facade.retrieveProxy(InRoomPlayerDataProxy.NAME) as InRoomPlayerDataProxy;
			this._arrChatTip=[this.mcTip.cardType1, this.mcTip.cardType2, this.mcTip.cardType3, this.mcTip.cardType4, this.mcTip.cardType5, this.mcTip.cardType6, this.mcTip.cardType7, this.mcTip.cardType8, this.mcTip.cardType9];
			var index:int=0;
			while (index < 9)
			{
				this._arrChatTip[index].alpha=0;
				this._arrChatTip[index].visible=false;
//				this.initChatTip((index + 1));
				index=index + 1;
			}
			return;
		}

		override public function listNotificationInterests():Array
		{
			return [CLZNoticeName.CHAT_RES, CLZNoticeName.STAND_UP_RSP, CLZNoticeName.SHOW_CARD_TIP];
		}

		override public function handleNotification(notification:INotification):void
		{
			var data:Object=notification.getBody();
			var playerVo:PlayerVo=null;
			var _loc_4:Object=null;
			switch (notification.getName())
			{
				case CLZNoticeName.CHAT_RES:
				{
					playerVo=this._inRoomData.getSitPlayerByMid(data.userid) as PlayerVo;
					if (playerVo != null)
					{
					}
					if (data.type == 0)
					{
						this.showChatTip(playerVo.seatId, data.msg);
					}
					break;
				}
				case CLZNoticeName.STAND_UP_RSP:
				{
					if (data.seatId < 1 || data.seatId > 10)
						return;
					this.hideChatTip(data.seatId);
					break;
				}
				case CLZNoticeName.SHOW_CARD_TIP:
				{
					this.showChatTip(data.seatId, data.msg);
					break;
				}
				default:
				{
					break;
				}
			}
			return;
		}

		private function showChatTip(chatTipId:int, msg:String):void
		{
			if (chatTipId < 1 || chatTipId > 9)
				return;
			if (chatTipId == 0)
			{
				return;
			}
			var msgLength:*=TextControl.checkStrByte(msg);
			if (msgLength > 24)
			{
				msg=msg.substr(0, 11) + "…";
			}
			GameData.removeNumChildre(this._arrChatTip[(chatTipId - 1)].mc);
			var txt:TextField=new TextField();
			txt.autoSize=TextFieldAutoSize.LEFT;
			txt.selectable=false;
			txt.textColor=16777215;
			txt.cacheAsBitmap=true;
			txt.text=msg;
			if (txt.width > this._maxWidth)
			{
				txt.wordWrap=true;
				txt.width=this._maxWidth;
			}
			this._arrChatTip[(chatTipId - 1)].mc.addChild(txt);
			txt.x=(this._maxWidth - txt.width) * 0.5;
			txt.y=(this._maxHeight - txt.height) * 0.5;
			this._arrChatTip[(chatTipId - 1)].visible=true;
			TweenLite.to(this._arrChatTip[(chatTipId - 1)], 0.8, {alpha: 1, onComplete: this.onFinishCardTypeTween, onCompleteParams: [chatTipId]});
			return;
		}

		private function showChatTip0(chatTipId:int, msg:String):void
		{
			if (chatTipId < 1 || chatTipId > 9)
				return;
			if (chatTipId == 0)
			{
				return;
			}
			var _loc_3:*=TextControl.checkStrByte(msg);
			if (_loc_3 > 6)
			{
				msg=msg.substr(0, 3) + "…";
			}
			this._arrChatTip[(chatTipId - 1)].txt.text=msg;
			this._arrChatTip[(chatTipId - 1)].visible=true;
			TweenLite.to(this._arrChatTip[(chatTipId - 1)], 0.8, {alpha: 1, onComplete: this.onFinishCardTypeTween, onCompleteParams: [chatTipId]});
			return;
		}

		private function onFinishCardTypeTween(chatTipId:int):void
		{
			TweenLite.to(this._arrChatTip[(chatTipId - 1)], 0.8, {delay: 2, alpha: 0, onComplete: this.onFinishHideCardTypeTween, onCompleteParams: [chatTipId]});
			return;
		}

		private function onFinishHideCardTypeTween(chatTipId:int):void
		{
			this.hideChatTip(chatTipId);
			return;
		}

		/**
		 * 注册之后，隐藏9个聊天提示
		 * @param chatTip
		 */
		private function hideChatTip(chatTipId:int):void
		{
			TweenLite.killTweensOf(this._arrChatTip[(chatTipId - 1)]);
//			initChatTip(chatTipId);
			this._arrChatTip[(chatTipId - 1)].alpha=0;
			this._arrChatTip[(chatTipId - 1)].visible=false;
			this.sendNotification(CLZNoticeName.HIDE_CARD_TIP, {seatId: chatTipId});
			return;
		}

		private function initChatTip(chatTipId:int):void
		{
			this._arrChatTip[(chatTipId - 1)].txt.text="";
			this._arrChatTip[(chatTipId - 1)].alpha=0;
			this._arrChatTip[(chatTipId - 1)].visible=false;
		}


		private function get mcTip():MovieClip
		{
			return viewComponent as MovieClip;
		}

	}
}
